package org.csu.laboratoryequipment.controller;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.User;
import org.csu.laboratoryequipment.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService userService;

    //获取所有users
    @GetMapping("users")
    @ResponseBody
    public CommonResponse<List<User>> Get(){

        return userService.getUserList();
    }

    //根据userid获取某个操作人员
    @GetMapping("users/{id}")
    @ResponseBody
    public CommonResponse<User> Get(@PathVariable("id") String userid){
        return userService.getUserById(userid);
    }

    //删除某个user
    @DeleteMapping("users/{id}")
    @ResponseBody
    public CommonResponse<List<User>> DELETE(@PathVariable("id") String id){
        return userService.deleteUserById(id);
    }

    //添加一个user
    @PostMapping("users")
    @ResponseBody
    public CommonResponse<List<User>> POST(
            @RequestParam("userid") String userid,
            @RequestParam("name") String name,
            @RequestParam("department") String department,
            @RequestParam("age") int age
    ){
        return userService.postUser(userid,name,department,age);
    }

    //修改
    @PutMapping("users")
    @ResponseBody
    public CommonResponse<List<User>> PUT(
            @RequestParam("userid") String userid,
            @RequestParam("name") String name,
            @RequestParam("department") String department,
            @RequestParam("age") int age
    ){
        return userService.putUser(userid,name,department,age);
    }



}
